var config = require('../../../../utils/config.js');
var util = require('../../../../utils/util');
var API_URL = config.API_URL;
var userInfo;
function getCode(that) {
    my.httpRequest({
        url: API_URL.send_sms,
        data: {
            from: 'cloudMasses',
            type: that.data.typ,
            mobile: that.data.mobile,
            user_id: userInfo.userId
        },
        method: 'POST',
        success: function (res) {
            console.log(res)
            that.setData({
                key: res.data.data.key,
                code_time: 120,
                code_disabled: true
            })
            util.countdown(that, 120);
        },
        fail: function (res) { },
        complete: function (res) { },
    })
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        code: '',
        code_text: [' ', ' ', ' ', ' ', ' ', ' '],
        keyboard: false,
        disable: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        // 拿到传过来的电话号码 和 设置类型（修改和设置）
        console.log(options)
        var mobile = options.tel;
        var tag = options.tag;
        var typ;
        // 判断是修改还是设置密码
        switch (tag) {
            case 'set': typ = 2; break;
            case 'edit': typ = 3; break;
            case 'forget': typ = 4; break;
        }
        userInfo = my.getStorageSync({ key: 'user' }).data;
        // 对电话号码加密
        var mobile_str = util.changeStr(mobile, 4, 4, '****');
        that.setData({
            mobile: mobile,
            mobile_str: mobile_str,
            typ: typ,
            tag: tag
        })
        getCode(that);
    },
    code: function (e) {
        var code = e.detail.value;
        // 获取输入的字符长度
        var length = code.length;
        var disable;
        console.log(e);
        var code_text;
        console.log(length)
        switch (length) {
            case 0: code_text = [' ', ' ', ' ', ' ', ' ', ' ']; disable = true; break;
            case 1: code_text = ['●', ' ', ' ', ' ', ' ', ' ']; disable = true; break;
            case 2: code_text = ['●', '●', ' ', ' ', ' ', ' ']; disable = true; break;
            case 3: code_text = ['●', '●', '●', ' ', ' ', ' ']; disable = true; break;
            case 4: code_text = ['●', '●', '●', '●', ' ', ' ']; disable = true; break;
            case 5: code_text = ['●', '●', '●', '●', '●', ' ']; disable = true; break;
            case 6: code_text = ['●', '●', '●', '●', '●', '●']; disable = false; break;
        }
        this.setData({
            code: code,
            code_text: code_text,
            disable: disable
        })
    },
    // 点击的时候失焦
    keyboard: function (e) {
        this.setData({
            keyboard: true
        })
    },
    // 重新获取验证码 时间过期后可以再次获取
    get_code: function (e) {
        var that = this;

        if (!that.data.code_disabled) {
            that.setData({
                code: "",
                getcode: ""
            })
            console.log(that.data)
            getCode(that)
        }
    },
    next: function (e) {
        var that = this;
        // 获取输入的code 和发送信息后台返回的key
        var code = that.data.code;
        my.httpRequest({
            url: API_URL.check_sms,
            data: {
                key: that.data.key,
                code: code
            },
            method: 'POST',
            success: function (res) {
                console.log(res)
                if (res.data.status == 200) {
                    my.navigateTo({
                        url: '/pages/user/pay/setPassword/setPassword?tag=' + that.data.tag,
                    })
                } else {
                    util.showNotice('验证码错误');
                }
            },
            fail: function (res) {
                util.showNotice('网络超时');
            },
            complete: function (res) { },
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

})